(function(Z){
	LightBox = function(thumbs,getSource,getDescription){
		var self = this;
		this.init = false;
		this.ui = '<div class="z-light-box">\
                        <div class="z-image-container"></div>\
                        <div class="z-description">\
                            <div class="z-background"></div>\
                            <div class="z-content"></div>\
                        </div>\
                        <a href="javascript:void(0);" class="z-prev"></a>\
                        <a href="javascript:void(0);" class="z-next"></a>\
                        <a href="javascript:void(0);" class="z-show">Desc</a>\
                        <a href="javascript:void(0);" class="z-close">close</a>\
                    </div>';
		this.thumbs = $(thumbs);
		this.getSource = getSource || function(thumb){
			return $(thumb).find('img').attr('src').replace(/_thumb\.(jpg|jpeg|gif|bmp|png)$/,'.$1');
		};
		this.getDescription = getDescription || function(thumb){
			return $(thumb).attr('title');
		};
		this.index = 0;
		this.length = $(thumbs).length;

		this.dialog = null;
		this.loading = null;

		$(thumbs).each(function(index){
			$(this).live('click',function(){
				self.index = index;
				if(!self.init){
					self.dialog = new Z.Dialog({
						content:self.ui
					});
					self.dialog.find('.z-description .z-background').css('opacity',0.8);

					self.dialog.open(function(){
						if(self.length >1 ){
							self.dialog.find('a.z-next').addClass('active');
						}

						self.go(self.index);

						self.dialog.find('.z-description .z-content').html(self.getDescription($(this)));

						self.dialog.find('.z-description').hide();

						self.dialog.find('a.z-prev').click(function(){
							self.prev();
							$(this).trigger('blur');
						});
						self.dialog.find('a.z-next').click(function(){
							self.next();
							$(this).trigger('blur');
						});
						self.dialog.find('a.z-close').click(function(){
							self.dialog.close();
						});

						self.dialog.find('a.z-show').click(function(){
							self.dialog.find('.z-description').toggle();
							if(self.dialog.find('.z-description:visible').length > 0){
								Z.moveScroll(self.dialog.find('.z-description .z-content'));
							}
						});
					});
				}else{
					self.go(self.index);
				}
			})
		});
	};

	LightBox.prototype.go = function(){
		var self = this,
		src = this.getSource(this.thumbs.eq(this.index)),
		other = $.browser.msie?'onabort="this.src=this.src;"':'';
		if(this.init){
			this.dialog.find('.z-light-box .z-image-container img').fadeOut(200,function(){
				$(this).html('');
			});
		}
		self.dialog.find('.z-description').hide();

		this.loading = Z.loading(this.dialog.find('.z-light-box'));

		this.dialog.find('.z-image-container').html('<img src="'+src+'" alt="" style="display:none;" '+other+'/>');

		this.dialog.find('.z-image-container img').load(function(){
			self.animate(Z.LightBox.calculateSize($(this)));
		});

		if(this.index <= 0){
			this.dialog.find('.z-prev').removeClass('active');
			this.dialog.find('.z-next').not('.active').addClass('active');
		}else if(this.index >= this.length -1){
			this.dialog.find('.z-next').removeClass('active');
			this.dialog.find('.z-prev').not('.active').addClass('active');
		}else{
			this.dialog.find('.z-prev').not('.active').addClass('active');
			this.dialog.find('.z-next').not('.active').addClass('active');
		}
	};

	LightBox.prototype.next = function(){
		//this.index = (this.index >= this.length - 1)?0:(this.index + 1);

		if(this.index < this.length - 1){
			this.index++;
			this.go();
		}
	};

	LightBox.prototype.prev = function(){
		//this.index = (this.index <= 0)?(this.length-1):(this.index-1);

		if(this.index > 0){
			this.index--;
			this.go();
		}
	};

	LightBox.prototype.animate = function(size){
		var self = this;
		this.dialog.find('.z-image-container img').width(size.width);
		this.dialog.find('.z-image-container img').height(size.height);

		this.dialog.find('.z-light-box').show().animate({
			'width':size.width+'px',
			'margin-left':-size.width/2+'px'
		},300,function(){
			$(this).animate({
				'height': size.height+'px',
				'margin-top':-size.height/2+'px'
			}, 300, function(){
				self.loading.fadeOut(500);
				$(this).find('.z-image-container img').show();
			})
		});
	};

	LightBox.calculateSize = function(image){
		var imageWidth = $(image).width(), imageHeight = $(image).height(),
		windowWidth = $(window).width(), windowHeight = $(window).height(),
		w = imageWidth, h = imageHeight;

		if(imageWidth > windowWidth){
			w = windowWidth * 0.7;
			h = w * imageHeight / imageWidth;
		}
		if(imageHeight > windowHeight){
			h = windowHeight * 0.7;
			w = h * imageWidth / imageHeight;
		}
		return {
			width:w,
			height:h
		};
	};

	Z.LightBox = LightBox;
})(window.Z);